package inventory

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func inventoryPurchase(inventory *gin.RouterGroup) {
	//销售
	stock := inventory.Group("/purchase")
	{
		stock.POST(sql.RequestAdd, inventoryAdd)
	}
}

// InventoryAdd /** 商品采购
func inventoryAdd(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.Inventory
		if err := c.Bind(&bean); err != nil {
			db.Error = err
			return db, nil
		}
		if bean.ProductId == 0 || bean.PurchasePrice == 0 || bean.PurchaseNumber == 0 ||
			bean.PurchaseWeight == 0 || bean.TotalAmount == 0 {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		bean.InventoryWeight = bean.PurchaseWeight
		bean.InventoryNumber = bean.PurchaseNumber
		db.Create(&bean)
		return db, bean
	})
}
